package cn.cmcc.online.smsapi.sdk.queue;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.json.JSONObject;

import android.content.Intent;
import cn.cmcc.online.smsapi.sdk.constant.Constant;
import cn.cmcc.online.smsapi.sdk.log.LogManager;
import cn.cmcc.online.smsapi.sdk.util.JsonUtil;
import cn.cmcc.online.smsapi.util.ParseSmsToBubbleUtil;

public class BubbleTaskQueue {
    private static BlockingQueue<JSONObject> queue = new LinkedBlockingQueue<JSONObject>();// 阻塞队列

    /**
     * 
     * @param dataStatu
     *            default 0
     * @param msgId
     * @param phoneNum
     * @param smsContent
     * @param centerNum
     * @param smsReceiveTime
     * @param dataType
     *            1:simple bubble 2:rich bubble 3:all
     * @param res
     */
    public static void addDataToQueue(int dataStatu, String msgId,
            String phoneNum, String smsContent, String centerNum,
            long smsReceiveTime, int dataType, JSONObject res) {
//        if (res == null)
//            res = new JSONObject();
//        try {
//            res.put("dataStatu", dataStatu);
//            res.put("msg_id", msgId);
//            res.put("phoneNum", phoneNum);
//            res.put("smsContent", smsContent);
//            res.put("smsReceiveTime", smsReceiveTime);
//            if (centerNum != null)
//                res.put("centerNum", centerNum);
//            res.put("dataType", dataType);
//            queue.put(res);
//            startTaskQueue();
//        } catch (Throwable e) {
//            e.printStackTrace();
//        }
//        BubbleTaskQueue.addDataToQueue(0, msgId, phoneNum, bodyText, centerNum,  msgs[0].getTimestampMillis(), 3, null);
    	addDataToQueue(dataStatu,msgId,phoneNum,smsContent,centerNum,smsReceiveTime,dataType,res, 0);
    }
    
    //isNeedNotify为1时，需要通知
    public static void addDataToQueue(int dataStatu, String msgId,
            String phoneNum, String smsContent, String centerNum,
            long smsReceiveTime, int dataType, JSONObject res,int isNeedNotify) {
        if (res == null)
            res = new JSONObject();
        try {
            res.put("dataStatu", dataStatu);
            res.put("msg_id", msgId);
            res.put("phoneNum", phoneNum);
            res.put("smsContent", smsContent);
            res.put("smsReceiveTime", smsReceiveTime);
            if (centerNum != null)
                res.put("centerNum", centerNum);
            res.put("dataType", dataType);
            
            res.put("isNeedNotify", isNeedNotify);
            queue.put(res);
            startTaskQueue();
        } catch (Throwable e) {
//            e.printStackTrace();
            LogManager.e(Constant.TAG, "addDataToQueue: ", e);

        }

    }

    static Thread queueThread = null;

    public synchronized static void startTaskQueue() {
        if (queueThread == null) {
            queueThread = new Thread() {
                public void run() {
                	try {
						this.setName("xiaoyuan_taskbubblequeue");
					android.os.Process.setThreadPriority(SdkTaskQueue.XIAOYUAN_THREA_PRIORITY);
                    JSONObject task = null;
                    while (true) {
                        try {
                            task = queue.take();
                            if (task != null) {
                                loadCache(task);
                            }
                        } catch (Throwable e) {
                            LogManager.e("BubbleTaskQueue", e.getLocalizedMessage(),e);
                        }
                    }
					} catch (Throwable e) {
						LogManager.e(Constant.TAG, e.getMessage(), e);
					}
                	};
            };
            queueThread.start();
        }
    }
    private static void loadCache(JSONObject task) throws Exception {
        Integer dataType = (Integer) JsonUtil.getValueFromJsonObject(task,"dataType");   
        /*SDK-394/songzhirong/2016.04.18---start---*/

        ParseSmsToBubbleUtil.parseSmsToBubbleResultMap(
                (String) JsonUtil.getValueFromJsonObject(task,"msg_id"),
                (String) JsonUtil.getValueFromJsonObject(task,"phoneNum"),
                (String) JsonUtil.getValueFromJsonObject(task,"smsContent"),
                (String) JsonUtil.getValueFromJsonObject(task,"centerNum"),
                (Long) JsonUtil.getValueFromJsonObject(task, "smsReceiveTime"),
                dataType, true, true,getExtendMap(task));
        /*SDK-394/songzhirong/2016.04.18---end---*/

        try {
            Integer isNeedNotify = (Integer) JsonUtil.getValueFromJsonObject(task,
                    "isNeedNotify");
			
            if(isNeedNotify!=null&&1==isNeedNotify){
             	try {
             		
					Intent intent= new Intent("cn.com.xy.douqu.reflashlist");            			
    				Constant.getContext().sendBroadcast(intent);
    				
				
             	} catch (Throwable e) {
//					e.printStackTrace();
                    LogManager.e(Constant.TAG, "loadCache: ", e);

				}
            }
            
        } catch (Throwable e) {
            // TODO: handle Throwable
        }
    }
    /*SDK-394/songzhirong/2016.04.18---start---*/

    private static Map<String, String>  getExtendMap(JSONObject task){
    	try {
    	     Map<String, String> map = new HashMap<String, String>();
    	        map.put("isUseNewAction", JsonUtil.getValueFromJsonObject(task,"isUseNewAction")+"");
    	        map.put("msgTime", JsonUtil.getValueFromJsonObject(task,"smsReceiveTime")+"");
    	        return map;
    	        
		} catch (Throwable e) {
			LogManager.e(Constant.TAG, "getExtendMap=", e);
		}
    	return null;
    }
    /*SDK-394/songzhirong/2016.04.18---end---*/

}
